using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using AjaxPro;
using NExt.Serialization.Core;

namespace NExt.Serialization.AjaxPro.Conversion
{
    internal static class SerilizationHelper
    {
        internal static bool IsSerializableValue(object value)
        {
            if (value == null)
                return false;

            if (value is string)
                if (string.IsNullOrEmpty(value as string))
                    return false;

            if (value is IList)
                if (((IList)value).Count == 0)
                    return false;

            return true;
        }

        internal static bool SerializeProperties(object objectValue, StringBuilder sb, bool b, IEnumerable<PropertyInfo> propertyInfos)
        {
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                MethodInfo methodInfo = propertyInfo.GetGetMethod();
                if (methodInfo == null)
                    continue;

                if (methodInfo.GetParameters().Length > 0)
                    continue;

                string propertyName = propertyInfo.Name;
                object propertyValue = methodInfo.Invoke(objectValue, null);

                if (propertyName.Equals("extended"))
                    continue;

                //TODO: Onur EKER
                if (!IsSerializableValue(propertyValue))
                    continue;

                string serializedValue = global::AjaxPro.JavaScriptSerializer.Serialize(propertyValue);

                if (propertyName.Equals("listeners"))
                    if (serializedValue.Equals("{}"))
                        continue;

                if (!b)
                    sb.Append(",");

                JavaScriptUtil.QuoteString(propertyName, sb);
                sb.Append(':');
                sb.Append(serializedValue);
                b = false;
            }
            return b;
        }

        internal static bool SerializeVariables(object objectValue, StringBuilder sb, IEnumerable<FieldInfo> fieldInfos)
        {
            bool b;
            IList<KeyValuePair<string, string>> variables = new List<KeyValuePair<string, string>>();
            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                string variableName = fieldInfo.Name;
                object variableValue = fieldInfo.GetValue(objectValue);


                if (!(variableValue is global::NExt.Core.Model.Base.Component))
                    continue;

                string generatedId = IdGenerator.GetId();
                (variableValue as global::NExt.Core.Model.Base.Component).varId = generatedId;

                variables.Add(new KeyValuePair<string, string>(variableName, generatedId));
            }

            if (IsSerializableValue(variables))
            {
                JavaScriptUtil.QuoteString("vars", sb);
                sb.Append(':');
                global::AjaxPro.JavaScriptSerializer.Serialize(variables, sb);
                b = false;
            }
            else
            {
                b = true;
            }
            return b;
        }
    }
}